/*
 * @Author: lwHao
 * @Date: 2020-09-20 14:01:00
 * @LastEditors: lwHao
 * @LastEditTime: 2020-09-20 17:06:29
 */
import axios from 'axios'

export const request = axios.create({
  baseURL: 'https://conduit.productionready.io'
})
export default (context) => {
  let {store,app} = context
  request.interceptors.request.use(function (config) {
    const { user } = store.state
    if (user && user.token) {
      config.headers.Authorization = `Token ${user.token}`
    }
    return config
  }, function (error) {
    return Promise.reject(err)
  })
  request.interceptors.response.use(function (response) {
    return response
  },function(error) {
    if (error.response.status === 401){
      app.router.push('/login')
    }
    return Promise.reject(error)
  })
}
